package com.own.component.log.operation.core;

import com.alibaba.fastjson2.JSON;
import com.own.component.log.operation.entity.LogOperationEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * ExtraLogOperationSpringHook
 *
 * @author chenxueli
 * @date 2022-09-12 13:29:00
 */
@Slf4j
@Component
public class PersistenceLogOperationSpringHook {

    @Resource
    private List<PersistenceLogOperationHook> persistenceLogOperationHookList;

    @PostConstruct
    private void init() {
        var list = persistenceLogOperationHookList.stream().map(item -> item.getClass().getName()).toList();
        log.info("操作日志持久化 ===>【{}】", JSON.toJSONString(list));
    }

    /**
     * 获取对应的日志操作接口
     *
     * @param entity 对象信息
     */
    public void run(LogOperationEntity entity) {
        persistenceLogOperationHookList.forEach(hook -> hook.run(entity));
    }

}
